package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.Batches;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;
import com.luck.easyrecyclerview.swipe.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class BatchListAdapter extends RecyclerView.Adapter<BatchListAdapter.ItemViewHolder> {
    private Context mContext;
    private List<List<Batches.DataBean>> list = new ArrayList<>();

    public OnUpdateBatch updateBatch;

    public interface OnUpdateBatch {
        void onBatch(List<Batches.DataBean> beanList);
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.batch_viewgroup, parent, false);
        return new ItemViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        final List<Batches.DataBean> beanList = list.get(position);
        Batches.DataBean bean = beanList.get(0);// 取第一个的
        String due_date = bean.getDue_date();
        String stock = bean.getStock();
        String unit = bean.getUnit();
        if (!Tools.isNull(due_date)) {
            holder.tv_time.setText(Tools.SubTime2(due_date + ""));
        } else {
            holder.tv_time.setText("1970-01-01");
        }
        if (!Tools.isNull(stock)) {
            holder.tv_stock.setText(stock);
        } else {
            holder.tv_stock.setText("");
        }
        holder.tv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 编辑批次
                updateBatch.onBatch(beanList);
            }
        });
        MyLayoutManager manager = new MyLayoutManager(mContext);
        holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
        holder.recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5f), mContext.getResources().getColor(R.color.line_color)));
        holder.recyclerView.setLayoutManager(manager);
        BatchItemAdapter adapter = new BatchItemAdapter(mContext, position, beanList);
        holder.recyclerView.setAdapter(adapter);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setList(List<List<Batches.DataBean>> list) {
        this.list = list;
    }

    public BatchListAdapter(Context context, List<List<Batches.DataBean>> list, OnUpdateBatch updateBatch) {
        this.mContext = context;
        this.list = list;
        this.updateBatch = updateBatch;
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;
        TextView tv_update, tv_time, tv_stock;

        public ItemViewHolder(View view) {
            super(view);
            recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
            tv_update = (TextView) view.findViewById(R.id.tv_update);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_stock = (TextView) view.findViewById(R.id.tv_stock);
        }
    }
}

